package as3.component.support
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	
	import as3.component.Component;
	import as3.component.controls.Label;
	import as3.component.controls.PanelCloseButton;

	public class PanelTitle extends Component
	{
		
		protected var _bg:DisplayObject;
		protected var _titleString:DisplayObject;
		public var _closeBtn:PanelCloseButton;
		
		protected var _fontColor:uint = 0;
		protected var _fontSzie:uint = 12;
		protected var _fontName:String = "宋体";
		protected var _fontBold:Boolean = false;
		protected var _showCloseBtn:Boolean = true;
		
		protected var _label:Label;
		
		private var _title:String;
		
		private var defaultHeight:Number = 25;
		
		private var titleResString:String;
		
		public function PanelTitle(res:String)
		{
			titleResString = res;
			super();
		}
		
		override protected function initUI():void
		{
			super.initUI();
			_uiLibrary["bg"] = "PanelTitleBackgroundSkin";
			_uiLibrary["titleString"] = titleResString;
			
			_fontColor = _defaultSkin.panelTabel.titleFontColor || 0;
			_fontBold = _defaultSkin.panelTabel.titleFontBold || false;
			_fontSzie = _defaultSkin.panelTabel.titleFontSize || 12;
			_fontName = _defaultSkin.panelTabel.titleFontname || "宋体";
		}
		
		override protected function initCom():void
		{
			super.initCom();
//			setMasked(true);
			_label = new Label();
			_label.color = _fontColor;
			_label.bold = _fontBold;
			_label.fontSize = _fontSzie;
			_label.fontName = _fontName;
			_label.selectable = _label.mouseChildren = _label.mouseEnabled = false;
			_label.filters = [new GlowFilter(0, 1, 2, 2, 3, 1)];
			addChild(_label);
			
			if (_showCloseBtn) {
				_closeBtn = new PanelCloseButton();
				addChild(_closeBtn);
				_closeBtn.addEventListener(MouseEvent.CLICK, $onCloseClick, false, 0, true);
			}
		}
		
		override protected function creatUI():void
		{
			super.creatUI();
			if(_titleString != null)
			{
				addChild(_titleString);
			}
			height = defaultHeight;
		}
		
		public function set bg(dis:DisplayObject):void
		{
			if (dis && _bg != dis) 
			{
				_bg = dis;
				setBackgroundView(_bg);
			}
		}
		
		public function set titleString(dis:DisplayObject):void
		{
			if (dis && _titleString != dis) 
			{
				_titleString = dis;
			}
		}
		
		public function set title(value:String):void
		{
			if (value == _title) return;
			_title = value;
			_label.text = _title;
			titleChange();
		}
		
		public function get title():String
		{
			return _title;
		}
		
		protected function titleChange():void
		{
			_label.y = 8;
			_label.x = (width - _label.width) * 0.5;
			if(_titleString != null)
			{
				_titleString.x = (width - _titleString.width) * 0.5;
				_titleString.y = 4;
			}
			_closeBtn.x = width  - _closeBtn.width - 8;
			_closeBtn.y = 8;
		}
		
		override public function set width(value:Number):void
		{
			_width = value;
//			super.width = value;
			_bg.width = value;
			sizeChange();
		}
		
		override public function set height(value:Number):void
		{
			_height = value;
//			super.height = value;
			_bg.height = value;
			sizeChange();
		}
		
		protected function sizeChange():void
		{
//			setMaskRect(new IntRectangle(0,0,width, height));
			titleChange();
		}
		
		private function $onCloseClick(evt:MouseEvent):void
		{
			dispatchEvent(new Event(Event.CLOSE));
		}
		
		public function set showCloseBtn(value:Boolean):void
		{
			if (_showCloseBtn == value) return;
			_showCloseBtn = value;
			if (_showCloseBtn) {
				_closeBtn.visible = true;
			} else {
				_closeBtn.visible = false;
			}
		}
		
		public function clear():void
		{
			_bg = null;
			_titleString = null;
			_closeBtn = null;
		}
		
	}
}